package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.SkillItemsSet;
import com.xmy.cultivate.entity.SkillShutnum;
import com.xmy.cultivate.entity.SkillStationsItems;
import com.xmy.cultivate.service.ISkillItemsSetService;
import com.xmy.cultivate.service.ISkillShutnumService;
import com.xmy.cultivate.service.ISkillStationsItemsService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Objects;

/**
 * <p>
 * 培训岗位、项目 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2025-08-21
 */
@RestController
@RequestMapping("/api/skillStationsItems")
public class SkillStationsItemsController {
    @Autowired
    ISkillStationsItemsService iSkillStationsItemsService;
    @Autowired
    ISkillShutnumService iSkillShutnumService;

    @GetMapping ("/getSkillStationsItemsList")
    public Result getSkillStationsItemsList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                           @RequestParam(value = "type", required = false,defaultValue = "1") String type, //1：教学艺术
                                           @RequestParam(value = "stationsId", required = false) String stationsId,
                                           @RequestParam(value = "itemsId", required = false) String itemsId,
                                           @RequestParam(value = "subjectsId", required = false) String subjectsId){
        IPage<SkillStationsItems> iPage = new Page<>(pageNum, pageSize);
//        QueryWrapper<SkillStationsItems> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
//        queryWrapper.eq(StringUtils.isNotBlank(staionsId), "staions_id", stationsId);
//        queryWrapper.eq(StringUtils.isNotBlank(itemsId), "items_id", itemsId);
//        queryWrapper.eq(StringUtils.isNotBlank(subjectsId), "subjects_id", subjectsId);
//        iSkillStationsItemsService.page(iPage, queryWrapper);
        iSkillStationsItemsService.findAll(iPage, itemsId, type, stationsId, itemsId, subjectsId);
        return new Result(Code.OK, new PageResult(iPage.getRecords(), iPage.getTotal(), iPage.getPages()));

    }


    @PostMapping("/addSkillStationsItems")
    public Result addSkillStationsItems(@RequestBody SkillStationsItems skillStationsItems){
        QueryWrapper<SkillStationsItems> qw = new QueryWrapper<>();
        qw.eq("type",  skillStationsItems.getType());
        qw.eq("stations_id",skillStationsItems.getStationsId());
        qw.eq("items_id",skillStationsItems.getItemsId());
        qw.eq("subjects_id",skillStationsItems.getSubjectsId());
        long count = iSkillStationsItemsService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }

        boolean save = iSkillStationsItemsService.save(skillStationsItems);
        if (!save){
            return new Result(Code.ERROR, null, "添加失败");
        }
        return new Result(Code.OK, null, "添加成功");
    }

    @PutMapping("/editSkillStationsItems")
    public Result editSkillStationsItems(@RequestBody SkillStationsItems skillStationsItems){
        SkillStationsItems stationsItemsOld = iSkillStationsItemsService.getById(skillStationsItems.getId());
        if (Objects.isNull(stationsItemsOld)){
            return new Result(Code.EXP_ERROR, null, "该数据不存在,编辑失败");
        }
        QueryWrapper<SkillStationsItems> qw = new QueryWrapper<>();
        qw.ne("id",skillStationsItems.getId());
        qw.eq("type",  stationsItemsOld.getType());
        qw.eq("stations_id",skillStationsItems.getStationsId());
        qw.eq("items_id",skillStationsItems.getItemsId());
        qw.eq("subjects_id",skillStationsItems.getSubjectsId());
        long count = iSkillStationsItemsService.count(qw);
        if (count > 0) {
            return new Result(Code.ERROR, null, "该数据已存在");
        }
        boolean update = iSkillStationsItemsService.updateById(skillStationsItems);
        if (!update){
            return new Result(Code.ERROR, null, "编辑失败");
        }
        return new Result(Code.OK, null, "编辑成功");
    }



    @DeleteMapping("/deleteSkillStationsItems")
    public Result deleteSkillStationsItems(@RequestBody SkillStationsItems skillStationsItems){
        SkillStationsItems skillStationsItemsOld = iSkillStationsItemsService.getById(skillStationsItems.getId());
        if (Objects.isNull(skillStationsItemsOld)){
            return new Result(Code.ERROR, null, "该数据不存在");
        }
        QueryWrapper<SkillShutnum> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id",skillStationsItemsOld.getId());
        queryWrapper.eq("stations_id", skillStationsItemsOld.getStationsId());
        queryWrapper.eq("subjects_id", skillStationsItemsOld.getSubjectsId());
        queryWrapper.eq("deleted",0);
        long count = iSkillShutnumService.count(queryWrapper);
        if (count > 0){
            return new Result(Code.ERROR, null, "该数据有绑定数据,无法删除");
        }
        boolean delete = iSkillStationsItemsService.removeById(skillStationsItems);
        if (!delete){
            return new Result(Code.ERROR, null, "删除失败");
        }
        return new Result(Code.OK, null, "删除成功");
    }

}
